<?php
namespace app\easytask\controller;

use app\user\controller\AdminAssetController;
use cmf\controller\HomeBaseController;

use plugins\dingding\lib\Dingding;
use think\Db;

class IndexController extends HomeBaseController
{

    protected $PHONE_NUMBER = '17805902037';
    public function index()
    {

        $corp = session('corp');
        $app = session('app');
        $token = session('token');
        if(!empty($token))
            $this->assign('token',$token);
        $ding = new Dingding($corp,$app);

        $this->assign('corpid',$ding->getCorpid());

        $user = cmf_get_current_user();
        $category = $this->request->param('category');

        #测试user功能专用
        if(!$user)$user['mobile'] = $this->PHONE_NUMBER;
        $list = Db::name('receive');
        $keyword = $this->request->param('keyword');
        if(!empty($keyword))$list = $list->whereLike('task_content','%'.$keyword.'%');

        if($category == null)
        {
            $list = $list->where('receiver = '.$user['mobile'].' AND (finished <>3 OR finished IS NULL)')->select()->order('creat_time','desc');

        }
        else
        {
            $list = $list->where('receiver = '.$user['mobile'].' AND (finished <>3 OR finished IS NULL)')->where('type',$category)->select()->order('creat_time','desc');
        }


        $this->assign('list',$list);
        $this->assign('keyword',$keyword);
        return $this->fetch('mobile');
    }
    public function fresh($task_id){
        $total = Db::name('receive')->where('task_id',$task_id)->count();
        $inner = Db::name('receive')->where('task_id',$task_id)->where('finished',3)->count();
        Db::name('tasks')->where('task_id',$task_id)->update(['total'=>$total,'inner_finished'=>$inner]);
    }
    public function reply()
    {
//        任务标签tag：0.通知  1.要回复内容  2.要回复内容和附件

        $id = $this->request->param('receiveid');
        $result = Db::name('receive')->where('receive_id',$id)->find();
        if ($result['finished'] != 3 && $result['tag'] != 0)
            #任务未完成且不是通知，将任务标识成已读但未完成
            Db::name('receive')->where('receive_id',$id)->update(['finished' => 2 ]);
        else {
            $t = date('Y-m-d H:i:s');
            $sql = Db::name('receive')->where('receive_id',$id)->find();
            if(empty($sql['single_finish_time'])) Db::name('receive')->where('receive_id',$id)->update(['finished' => 3 ,'single_finish_time'=>$t]);
            else Db::name('receive')->where('receive_id',$id)->update(['finished' => 3 ]);
        }
        $nickname = Db::name('user')->where('mobile',$result['publisher'])->find()['user_nickname'];


        $this->fresh($result['task_id']);

        $this->assign('list', $result);
        $this->assign('publisher', $nickname);

        $this->assign('opening', $result['tag']);
        return $this->fetch();
    }

    public function ReplyContent()
    {
        $receiveid = $this->request->param('receiveid');
        $backcontent = $this->request->param('back_content');
        $replykey = $this->request->param('reply_key');
        Db::name('receive')->where('receive_id',$receiveid)->update(['back_content' => $backcontent ]);
        Db::name('receive')->where('receive_id',$receiveid)->update(['finished' => 3 ]);
        $file_names = [];
        $data = $this->request->param();
        $count = 0;
        while(isset($data['file_urls'][$count])){
            $temp_name['url'] = $data['file_urls'][$count];
            $temp_name['name'] = $data['file_names'][$count];
            array_push($file_names,$temp_name);
            $count = $count+1;
        }
        $file_names = json_encode($file_names);
        $t = date('Y-m-d H:i:s');
        $data['single_finish_time']=$t;

        $data['file_names'] = $file_names;

        unset($data['file_urls'],$data['receiveid']);

        Db::name('receive')->where('receive_id',$receiveid)->update($data);
        $this->fresh($data['task_id']);

        Db::name('tasks')->where('task_id',$data['task_id'])->update(['finish_time'=>$t]);

        $this->success('回复成功',url('index'));
    }
    public  function replied()
    {
        $user = cmf_get_current_user();
        if(!$user)$user['mobile'] = $this->PHONE_NUMBER;
        $result = Db::name('receive')->where('receiver',$user['mobile'])->where('finished',3)->select();
        $this->assign('list',$result);
        return $this->fetch();
    }

    public function task()
    {
        return $this->fetch();
    }

    public function add_post(){
        if (IS_POST) {
            if(empty($_POST['term'])){
                $this->error("请至少选择一个分类！");
            }
            if(!empty($_POST['photos_alt']) && !empty($_POST['photos_url'])){
                foreach ($_POST['photos_url'] as $key=>$url){
                    $photourl=sp_asset_relative_url($url);
                    $_POST['smeta']['photo'][]=array("url"=>$photourl,"alt"=>$_POST['photos_alt'][$key]);
                }
            }
            $_POST['smeta']['thumb'] = sp_asset_relative_url($_POST['smeta']['thumb']);

            $_POST['post']['post_modified']=date("Y-m-d H:i:s",time());
            $_POST['post']['post_author']=get_current_admin_id();
            $article=I("post.post");
            $article['smeta']=json_encode($_POST['smeta']);
            $article['post_content']=htmlspecialchars_decode($article['post_content']);
            $result=$this->posts_model->add($article);
            if ($result) {
                foreach ($_POST['term'] as $mterm_id){
                    $this->term_relationships_model->add(array("term_id"=>intval($mterm_id),"object_id"=>$result));
                }

                $this->success("添加成功！");
            } else {
                $this->error("添加失败！");
            }

        }
    }
}